<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Home\Controller;

use Think\Controller;
use Com\DingMiandengAuth;
use Com\DingdingAuth;
/**
 * 后台用户控制器
 * 
 * @author jry <598821125@qq.com>
 */
class TaskwapController extends HomeController
{

	public function _initialize()
	{
		vendor('dingding.api.Auth');
		vendor('dingding.api.User');
	}
	/**
	 * 钉钉
	 * 获取用户信息
	 */
	public function get_userinfo()
	{
    		
		$Auth=new \Auth();
		$User=new \User();
		$accessToken = $Auth->getAccessToken();
		$code = I("code");
		$userInfo = $User->getUserInfo($accessToken, $code);
		//Log::i("[USERINFO]".json_encode($userInfo));
		session('DING_USER',json_decode($userInfo,true));
		echo json_encode($userInfo);
		//$this->$this->ajaxReturn( json_encode($userInfo));
	}
    public function index()
    {
    	    	
//     			$appid='dingoa2vwlygt70y6eagkn';
//     	    	$appsecret='aImsyWNJcwtL1hi7gOcmoGpntURM7u9RNhHMiYEAYazEGjp5_kdobGFbS3gZJcnE';
//     	    	$D=new DingMiandengAuth($appid, $appsecret);
//     	    	$token = $D->getAccessToken();
    	    	    	    	
//     	 		$code=I('get.code');
//     	 		//获取用户授权的持久授权码
//     	    	$param_code=array(
//     	    	    	"tmp_auth_code"=>$code
//     	    	);
// 	    	  	$persistent_code_re=$D->getDingRe("sns/get_persistent_code",$param_code,'POST');
// 	    	  	//获取用户授权的SNS_TOKEN
// 	    	  	$param_token=array(
// 	    	  			"openid"=>$persistent_code_re['openid'],
// 	    	  			"persistent_code"=>$persistent_code_re['persistent_code']
// 	    	  	);
// 	    	  	$get_sns_token_re=$D->getDingRe("sns/get_sns_token",$param_token,'POST');
// 	    	  	//获取会员信息
// 	    	  	$param_user=array(
// 	    	  			"sns_token"=>$get_sns_token_re['sns_token']
	    	  			
// 	    	  	);
// 	    	  	$userinfo_re=$D->getDingRe("sns/getuserinfo",'','GET',$param_user);
	    	  	
//     	  	dump($userinfo_re);
//     	exit;
    	$this->assign('meta_title', 'loading...');
    	$this->display();
		
    }
   
    // 新增信息
    public function mytask()
    {
    	$user_info=session('DING_USER');
//     	if (!$user_info) {
//     		$corpid=C('CORPID');
// 	    	$corpsecret=C('SECRET');
// 	    	$D=new DingdingAuth($corpid, $corpsecret);
// 	    	$token = $D->getAccessToken();
	    	
// 	    	$param=array(
// 	    			"code"=>I('get.code')
// 	    	);
// 	    	$user_info=$D->getDingRe("user/getuserinfo",'','GET',$param);
	    	
// 	    	session('DING_USER',$user_info);
//     	}
    	
    	//dump($user_info);exit;
    	$ding_id=$user_info['userid'];
    	$uid=M('Employee')->where(array('ding_id'=>$ding_id))->getField('id');
    	//未开始
    	$map2['uid']=$uid;
    	$map2['status']=2;
    	$task_list2 = M('Task')->page(! empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))->where($map2)->order('emergency_level asc,id desc')->select();
    	$page2 = new \Common\Util\Page(M('Task')->where($map2)->count(), C('ADMIN_PAGE_ROWS'));
    	foreach ($task_list2 as $k=>$v){
    		$task_list2[$k]['projectid']=M('Project')->where('id='.$v['projectid'])->getField('project_number');
    		$task_list2[$k]['uid']=M('Employee')->where('id='.$v['uid'])->getField('name');
    		$task_list2[$k]['emergency_level'] = M('Descriptions')->where(array('tablename'=>'emergency_level','fieldvalue'=>$v['emergency_level']))->getField('title');
    		$task_list2[$k]['type'] = M('Descriptions')->where(array('tablename'=>'task_type','fieldvalue'=>$v['type']))->getField('title');
    		$user = get_user_info($v['creater']);
    		$task_list2[$k]['creater']=$user['username'];
    	
    		$task_list2[$k]['start_time']?$task_list2[$k]['start_time']=date('Y-m-d H:i:s',$v['start_time']):'';
    		$task_list2[$k]['end_time']?$task_list2[$k]['end_time']=date('Y-m-d H:i:s',$v['end_time']):'';
    		
    	}
    	//进行中
    	$map1['uid']=$uid;
    	$map1['status']=1;
    	$task_list1 = M('Task')->page(! empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))->where($map1)->order('emergency_level asc,id desc')->select();
    	$page1 = new \Common\Util\Page(M('Task')->where($map1)->count(), C('ADMIN_PAGE_ROWS'));
    	foreach ($task_list1 as $k=>$v){
    		$task_list1[$k]['projectid']=M('Project')->where('id='.$v['projectid'])->getField('project_number');
    		$task_list1[$k]['uid']=M('Employee')->where('id='.$v['uid'])->getField('name');
    		$task_list1[$k]['emergency_level'] = M('Descriptions')->where(array('tablename'=>'emergency_level','fieldvalue'=>$v['emergency_level']))->getField('title');
    		$task_list1[$k]['type'] = M('Descriptions')->where(array('tablename'=>'task_type','fieldvalue'=>$v['type']))->getField('title');
    		$user = get_user_info($v['creater']);
    		$task_list1[$k]['creater']=$user['username'];
    	
    		$task_list1[$k]['start_time']?$task_list1[$k]['start_time']=date('Y-m-d H:i:s',$v['start_time']):'';
    		$task_list1[$k]['end_time']?$task_list1[$k]['end_time']=date('Y-m-d H:i:s',$v['end_time']):'';
    		
    	}
    	//已完结
    	$map3['uid']=$uid;
    	$map3['status']=3;
    	$task_list3 = M('Task')->page(! empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))->where($map3)->order('emergency_level asc,id desc')->select();
    	$page3 = new \Common\Util\Page(M('Task')->where($map3)->count(), C('ADMIN_PAGE_ROWS'));
    	foreach ($task_list3 as $k=>$v){
    		$task_list3[$k]['projectid']=M('Project')->where('id='.$v['projectid'])->getField('project_number');
    		$task_list3[$k]['uid']=M('Employee')->where('id='.$v['uid'])->getField('name');
    		$task_list3[$k]['emergency_level'] = M('Descriptions')->where(array('tablename'=>'emergency_level','fieldvalue'=>$v['emergency_level']))->getField('title');
    		$task_list3[$k]['type'] = M('Descriptions')->where(array('tablename'=>'task_type','fieldvalue'=>$v['type']))->getField('title');
    		$user = get_user_info($v['creater']);
    		$task_list3[$k]['creater']=$user['username'];
    	
    		$task_list3[$k]['start_time']?$task_list3[$k]['start_time']=date('Y-m-d H:i:s',$v['start_time']):'';
    		$task_list3[$k]['end_time']?$task_list3[$k]['end_time']=date('Y-m-d H:i:s',$v['end_time']):'';
    		
    	}
    	 
    	
    	
    	$status=I('status')?:2;   	
    	$this->assign('status',$status);
    	$this->assign('meta_title', '我的任务');
    	$this->assign('task_list2', $task_list2);
    	$this->assign('task_list1', $task_list1);
    	$this->assign('task_list3', $task_list3);
    	$this->display();
    }
    /**
     * 开始任务
     */
    public function starTask(){
    	$id=I('id');
    	$data=array(
    			'status'=>1,
    			'start_time'=>time()
    			
    	);
    	$task= M('Task')->where('id='.$id)->save($data);
    	
    	$this->success('您的任务已经开始！',U('mytask',array('status'=>1)));

    }
    /**
     * 完结任务
     */
    public function finishTask(){
    	$id=I('id');
    	$data=array(
    			'status'=>3,
    			'end_time'=>time()
    			 
    	);
    	$task= M('Task')->where('id='.$id)->save($data);
    	 
    	$this->success('您的任务已经完结！',U('mytask',array('status'=>3)));
    	
    }
    
    
    
}   
